package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsOrderMapper;
import com.qf.mapper.IndexTotalMapper;
import com.qf.pojo.DtsOrder;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.DayStatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author sunyamei
 * @version V1.0
 * @Project javase2203
 * @Package com.qf.service
 * @Description:
 * @Date 2022/7/26 11:37
 */
@Service
public class OrderServiceImpl implements  OrderService {
    @Autowired
    private DtsOrderMapper orderMapper;


    @Autowired
    private IndexTotalMapper indexTotalMapper;


    /**
     * 统计订单数量
     * @return
     */
    @Override
    public int orderCount() {
        //创建查询条件
        QueryWrapper<DtsOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);

        Integer count = orderMapper.selectCount(queryWrapper);
        return count;
    }

    /**
     * 统计商品分类和对应分类的销售额
     * @return
     */
    @Override
    public List<CategorySellAmts> categorySellCount() {
        List<CategorySellAmts> list = indexTotalMapper.categorySellCount();

        return list;
    }

    /**
     * 日期和对应的订单总额, 订单笔数
     * @return
     */
    @Override
    public List<DayStatis> orderAmts() {
        List<DayStatis> list = indexTotalMapper.orderAmts();
        return list;
    }

    /**
     * 分页查询
     * @param user_id
     * @param order_sn
     * @param order_status
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @Override
    public IPage<DtsOrder> queryByPage(Integer user_id, String order_sn, String order_status, Integer page, Integer limit, String sort, String order) {

        //创建分页对象
        IPage<DtsOrder> ipage = new Page<>(page,limit);


        //创建查询对象
        QueryWrapper<DtsOrder> queryWrapper = new QueryWrapper<>();

        //判断条件是否为空
        if (user_id != null) {
            queryWrapper.eq("user_id", user_id);
        }
       if (!StringUtils.isEmpty(order_sn)){
           queryWrapper.eq("order_sn", order_sn);
       }
        if (!StringUtils.isEmpty(order_status)){
            queryWrapper.eq("order_status", order_status);
        }


        //排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }
        }else {
            queryWrapper.orderByDesc(sort);
        }

        //调用mapper
        IPage<DtsOrder> iPage = orderMapper.selectPage(ipage, queryWrapper);

        return iPage;
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @Override
    public DtsOrder findById(Integer id) {

//        //创建查询条件
        QueryWrapper<DtsOrder> queryWrapper = new QueryWrapper<>();

        DtsOrder byId = orderMapper.selectById(id);

        return byId;
    }


}
